﻿using OVS.CoreDomain;
using OVS.CoreDomain.Business;
using OVS.CoreDomain.Data;
using OVS.Data;
using System.Collections.Generic;
namespace OVS.Business
{
    public class BusinessVoter : IBusinessVoter
    {
        private readonly IDalVoter _dalVoter;
        private readonly IBusinessRandomPassword _businessRandomPassword;
        private readonly IBusinessPassword _businessPassword;
        private readonly IBusinessEmail _businessEmail;

        public BusinessVoter (IDalVoter dalVoter)
        {
            _dalVoter = dalVoter;
        }

        public BusinessVoter(IDalVoter dalVoter,IBusinessRandomPassword businessRandomPassword,
            IBusinessPassword businessPassword, IBusinessEmail businessEmail)
        {
            _dalVoter = dalVoter;
            _businessRandomPassword = businessRandomPassword;
            _businessPassword = businessPassword;
            _businessEmail = businessEmail;
        }

        public BusinessVoter()
        {
            _dalVoter = new DalVoter();
            _businessRandomPassword = new BusinessRandomPassword();
            _businessPassword = new BusinessPassword();
            _businessEmail = new BusinessEmail();
        }

        public IVoter GetVoter(string voterId)
        {
            return _dalVoter.GetVoter(voterId);
        }

        public bool AddVoter(IVoter voter)
        {
            string password = _businessRandomPassword.Generate();
            string _encryptedPassword = _businessPassword.EncryptPassword(password);
            voter.Password = _encryptedPassword;
            IEmail email = new EMail();
            email.From = "admin@ovs.com";
            email.To = voter.EmailId;
            email.Subject = "Your OVS password!";
            email.Body = "Your OVS Password is as follows:" + password;
            bool success = _dalVoter.AddVoter(voter);
            _businessEmail.SendEmail(email);
            return success;
        }

        public bool UpdateVoter(IVoter voter)
        {
            return _dalVoter.UpdateVoter(voter);
        }

        public IList<IVoter> SearchVoters(IVoterSearchCriteria voterSearchCriteria)
        {
            return _dalVoter.SearchVoters(voterSearchCriteria);
        }



        #region IBusinessVoter Members


        public IVoter HaveVoted(IVoter voter, IElection election)
        {
            return _dalVoter.HaveVoted(election, voter);
        }

        #endregion

        #region IBusinessVoter Members


        public bool UpdatePassword(IVoter voter)
        {
            voter = _dalVoter.GetVoter(voter.VoterId);
            string password = _businessRandomPassword.Generate();
            string _encryptedPassword = _businessPassword.EncryptPassword(password);
            voter.Password = _encryptedPassword;
            IEmail email = new EMail();
            email.From = "admin@ovs.com";
            email.To = voter.EmailId;
            email.Subject = "Your OVS password!";
            email.Body = "Your OVS Password is as follows:" + password;
            bool success = _dalVoter.UpdatePassword(voter);
            _businessEmail.SendEmail(email);
            return success;
        }

        #endregion
    }
}
